<?php

/**
 * Class	: M_permissions
 * 
 * Table	: s_permissions
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_permissions extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	function getAll($group_id){
		$sql = "SELECT vu_tree_menus.menu_title as TREE_MENU_TITLE,
				s_permissions.PERM_ID, 
				".$group_id." AS PERM_GROUP, 
				vu_tree_menus.MENU_ID AS PERM_MENU, 
				vu_tree_menus.MENU_PARENT AS MENU_PARENT,
				IF(s_permissions.PERM_PRIV IS NULL,0,1) AS PERM_PRIV
			FROM vu_tree_menus
			LEFT JOIN s_permissions ON(s_permissions.PERM_MENU = vu_tree_menus.MENU_ID AND s_permissions.PERM_GROUP = ".$group_id.")";
		$query  = $this->db->query($sql)->result();
		
		
		$n = $this->db->get_where('s_menus',array('menu_parent'=> 0))->num_rows();
		
		$isi = array();
		
		for($i=0;$i<$n;$i++)
		{
			$data = new stdClass();
			$data->TREE_MENU_TITLE = $query[$i]->TREE_MENU_TITLE;
			$data->PERM_ID = $query[$i]->PERM_ID;
			$data->PERM_GROUP = $query[$i]->PERM_GROUP;
			$data->PERM_MENU = $query[$i]->PERM_MENU;
			$data->MENU_PARENT = $query[$i]->MENU_PARENT;
			$data->PERM_PRIV = $query[$i]->PERM_PRIV;
			array_push($isi,$data);
			
			foreach($query as $v)
			{
				if($v->MENU_PARENT != 0 && $v->MENU_PARENT==($i+1))
				{
					$data = new stdClass();
					$data->TREE_MENU_TITLE = '&nbsp;&nbsp;&nbsp;&nbsp;'.$v->TREE_MENU_TITLE;
					$data->PERM_ID = $v->PERM_ID;
					$data->PERM_GROUP = $v->PERM_GROUP;
					$data->PERM_MENU = $v->PERM_MENU;
					$data->MENU_PARENT = $v->MENU_PARENT;
					$data->PERM_PRIV = $v->PERM_PRIV;
					array_push($isi,$data);
				}
			}
		}
		
		$total  = $this->db->query($sql)->num_rows();
		$data = (array) $isi;
		
		/* $data   = array();
		foreach($query as $result){
			$data[] = $result;
		} */
		
		$json   = array(
						'success'   => TRUE,
						'message'   => "Loaded data",
						'total'     => $total,
						'data'      => $data
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: save
	 * 
	 * Untuk menambah data baru atau mengubah data lama
	 * 
	 * @param array $data
	 * @return json
	 */
	function save($data){
		$last   = NULL;
		
		if(sizeof($data) > 1){
			$group_id = $data[0]->PERM_GROUP;
			foreach ($data as $row){
				//$group_id = $row->PERM_GROUP;
				$datau = array();
				if($row->PERM_PRIV = 'R'){
					$datau['PERM_PRIV']	= 'RCUD';
				}else{
					$datau['PERM_PRIV']	= null;
				}
				
				$this->db->where('PERM_ID', $row->PERM_ID)->update('s_permissions', $datau);
			}	
			
		}else{
			$group_id = $data->PERM_GROUP;
			$datau = array();
			if($data->PERM_PRIV){
				$datau['PERM_PRIV']	= 'RCUD';
			}else{
				$datau['PERM_PRIV']	= null;
			}
			$this->db->where('PERM_ID', $data->PERM_ID)->update('s_permissions', $datau);
			
		}
		
		$json = $this->getAll($group_id);
		
		return $json;
	}
	
	/**
	 * Fungsi	: delete
	 * 
	 * Untuk menghapus satu data
	 * 
	 * @param array $data
	 * @return json
	 */
	function delete($data){
		$this->db->where('PERM_ID', $data->PERM_ID)->delete('s_permissions');
		
		$total  = $this->db->get('s_permissions')->num_rows();
		$last = $this->db->get('s_permissions')->result();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil dihapus',
						'total'     => $total,
						"data"      => $last
		);
		
		return $json;
	}
}


?>